package com.vhuomai.product.label.service.impl;

import com.vhuomai.product.label.dto.LabelProductReq;
import com.vhuomai.product.label.entity.BaseProductLabel;
import com.vhuomai.product.label.mapper.LabelProductMapper;
import com.vhuomai.product.label.service.LabelProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class LabelProductServiceImpl implements LabelProductService {
    @Autowired
    private LabelProductMapper labelProductMapper;

    @Override
    public void addProductLabel(LabelProductReq labelProductReq) {
        List<BaseProductLabel> baseProductLabels = new ArrayList<>();
        labelProductReq.getProductIds().forEach(productId -> {
            BaseProductLabel baseProductLabel = labelProductMapper.findByProduct(productId);
            if (baseProductLabel != null) {
                labelProductMapper.deleteOne(baseProductLabel.getId());
            }
            baseProductLabel = new BaseProductLabel();
            baseProductLabel.setId(null);
            baseProductLabel.setLabelId(labelProductReq.getLabelId());
            baseProductLabel.setProductId(productId);
            baseProductLabel.setCreateTime(LocalDateTime.now());
            baseProductLabel.setUpdateTime(LocalDateTime.now());
            baseProductLabels.add(baseProductLabel);
        });
        labelProductMapper.insertAll(baseProductLabels);
    }

    @Override
    public void deleteProductLabel(List<Integer> labelProductIds) {
       labelProductMapper.deleteAll(labelProductIds);
    }
}
